/***set
 *产品编辑，复制，删除
 * ****/
import goodsInfoBean from "../objectValue/GoodsInfoBean.js";

import goodsTlpCtrl from "./GoodsTlpCtrl.js";

import config from "../conf/Config.js";

import staticConst from "../const/StaticConst.js";

import eventCtrl from "../event/EventCtrl.js";

import lang from "../conf/Lang.js";

import https from "../https/Https.js";

import limit from "../limit/AccessCtrl.js";

import saveGdsBean from "../objectValue/SaveGdsBean.js";

import localStore from "../LocalStore.js";

import util from "../util/Util.js";


import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);
let that = Vue.prototype;

let api = that.$api;

const MAX_NUM = 9; //最大图片量

const PAGE_SIZE = 10; //

var goodsCtrl = {

	actType: -1, //

	saveGB: null,

	gdPhotoArr: [], //产品照片

	firstLevelCat: [], //一级分类

	secondLevelCat: [], //二级分类

	thirdLevelCat: [], //三级分类

	category: {}, //catid:已经选中的最底层分类，catid1：一级分类，catid2：二级分类，已经选中的分类，

	mapQty: {}, //库存数据

	specList: null, //规格列表,

	unitList: null, //单位-列表,

	priceList: null, //价格-列表,

	brandList: null, //品牌-列表,

	catList: null, //产品分类列表,

	tagList: null, //标签列表；

	attrList: null, //属性-列表,

	storeList: null, //仓库列表，

	putOnList: null, //云店列表，

	ucpList: null, //经手列表,

	goodsInfoBean: null, //产品信息

	saveGdInfo: null, //提交存储信息

	checkedSpecArr: [], // 选中的规格

	checkCostSpecArr: [], //选中的成本规格

	checkedStoreSpecArr: [], //选中的成本规格

	seleFirstArr: [], //选中一级

	seleSecondArr: [], //选中的二级

	selectedUnitArr: [], //选中的单位

	unitCoverArr: [], //单位计算存储数据。
	mapUnitCover: {}, //单位计算存储数据。

	minUnit: null, // 最小单位，

	mapPrice: {}, //价格对象

	mapCost: {}, //成本列表	brandSelectList : [], //选中品牌列表,

	moreGdDetail: [], //更多详情

	darftGoods: {}, //草稿产品

	onsaleYds: [], //添加产品保持云店，存草稿用

	keyCode: "", //扫码保存，找不到产品添加产品

	specLsNo: 0, //规格分页id

	kwSpec: "", //搜索关键字

	barndNo: 0, //品牌分页id

	kwBrand: "", //品牌搜索关键字

	sUMatchImgArr: [], //规格单位匹配sku图片

	isSys: false, //是否是爱搜网（系统）

	searSeSpceArr: [], //在搜索过程中选中的规则

	searSeBrandArr: [], //在搜索品牌过程选中的品牌

	isEditGoods: 0, //是否编辑产品

};

/*****清除数据*******/
goodsCtrl.clearData = function () {

	this.saveGB = null; //

	this.gdPhotoArr = []; //产品照片

	this.firstLevelCat = []; //一级分类

	this.secondLevelCat = []; //二级分类

	this.thirdLevelCat = []; //三级分类

	this.category = {}; //catid:已经选中的最底层分类，catid1：一级分类，catid2：二级分类，已经选中的分类，

	this.mapQty = {}; //库存数据

	this.mapPrice = {}; //价格对象

	this.mapCost = {}; //成本列表

	this.mapUnitCover = {}; //单位计算

	this.category = {}; //已经选中的分类

	this.specList = null; //规格列表,

	this.unitList = null; //单位-列表,

	this.priceList = null; //价格-列表,

	this.brandList = null; //品牌-列表,

	this.catList = null; //产品分类列表,

	this.attrList = null; //属性-列表,

	this.storeList = null; //参考列表

	this.ucpList = null; //经手列表,

	this.saveGdInfo = null; //提交存储信息

	this.checkedSpecArr = []; // 选中的规格

	this.checkCostSpecArr = []; //选中的成本规格

	this.checkedStoreSpecArr = []; //选中的成本规格

	this.seleFirstArr = []; //选中一级

	this.seleSecondArr = []; //选中的二级

	this.selectedUnitArr = []; //选中的单位

	this.unitCoverArr = []; //单位计算存储数据。

	this.minUnit = null; // 最小单位，

	this.putOn = {}; //上架

	this.moreGdDetail = []; //更多详情

	this.darftGoods = {}; //草稿产品

	this.onsaleYds = []; //添加产品保持云店，存草稿用

	this.specLsNo = 0; //规格分页id

	this.kwSpec = ""; //规格搜索关键字

	this.barndNo = 0; //品牌分页id

	this.kwBrand = ""; //品牌搜索关键字

	this.sUMatchImgArr = []; //规格单位匹配sku图片

	this.searSeSpceArr = []; //在搜索过程中选中的规则

	this.searSeBrandArr = []; //在搜索品牌过程选中的品牌

	this.isEditGoods = 0
}

/****选中的规格单位sku,弹出选择窗口用******/
goodsCtrl.getSUMatchImgList = function (index, skuIds) {
	let list = [];
	let s = this.getCheckedSpecArr();
	let u = this.getSelectedUnit();
	for (let i = 0; i < u.length; i++) //选中的单位
	{
		let uo = u[i];
		let temp = {};
		if (s && s.length > 0) //选中的规格
		{
			for (let j = 0; j < s.length; j++) {
				let so = s[j];
				let temp1 = {};
				let a = String(so.ids).split("|"); //so.ids
				temp1.id = a[a.length - 1] + "_" + uo.id;
				temp1.title = so.titles + "," + uo.title;
				temp1.checked = String(skuIds).indexOf(temp1.id) >= 0 ? true : false;
				list.push(temp1);
			}
		} else {
			//只有选中单位
			temp.id = uo.id;
			temp.title = uo.title;
			temp.checked = String(skuIds).indexOf(temp.id) >= 0 ? true : false;
			list.push(temp);
		}
	}
	let data = {};
	data.index = index;
	data.list = list;
	return data;
}

/****检测是否sku绑定的图片*****/
goodsCtrl.sanfCheckSkuForPhoto = function (data) {
	let idx = data.index;
	let list = data.list;
	this.gdPhotoArr[idx].skuIds = data.seleIds;
	this.gdPhotoArr[idx].skuTitles = data.seleTits;

	let len = this.gdPhotoArr.length;
	for (let i = 0; i < len; i++) {
		let sku = this.gdPhotoArr[i];
		if (i == idx || !sku.imgs || !sku.skuIds) continue;

		for (let j = 0; j < list.length; j++) {
			let id = list[j].id;
			let title = list[j].title;
			let index = sku.skuIds.indexOf(id);
			if (index >= 0) {
				let re = new RegExp(id, 'g');
				sku.skuIds = String(sku.skuIds).replace(re, "");
				let arr = String(sku.skuIds).split(",");
				let idsArr = [];
				let titsArr = [];
				for (let k = 0; k < arr.length; k++) {
					if (arr[k] == ",") continue;
					if (arr[k]) {
						let suId = arr[k];
						let suArr = suId.split("_");
						let tit = "";
						if (suArr.length == 2) {
							tit = this.getSpecUnitTitle(suArr.length, suArr[1], suArr[0]);
						} else {
							tit = this.getSpecUnitTitle(suArr.length, suArr[0]);
						}
						idsArr.push(suId);
						titsArr.push(tit);
					}
				}
				if (idsArr.length > 0) {
					sku.skuIds = idsArr.join(",");
				} else {
					sku.skuIds = "";
				}
				if (titsArr.length > 0) {
					sku.skuTitles = titsArr.join(",");
				} else {
					sku.skuTitles = "";
				}
			}
		}
	}
	return this.gdPhotoArr;
}

/****获取已经选择中的规格和单位title*****/
goodsCtrl.getSpecUnitTitle = function (len, uId, sId) {
	let s = this.getCheckedSpecArr();
	let u = this.getSelectedUnit();
	let sTitStr = "";
	let uTitStr = "";
	if (len == 2) {
		for (let i = 0; i < s.length; i++) {
			let so = s[i];
			let temp1 = {};
			// if(so.ids == sId)
			if (so.ids.indexOf(sId) >= 0) {
				sTitStr = so.titles;
				break;
			}
		}
	}

	for (let i = 0; i < u.length; i++) //选中的单位
	{
		let uo = u[i];
		let temp = {};
		if (uId == uo.id) {
			uTitStr = uo.title;
			break;
		}
	}
	let title = len == 2 ? sTitStr + "," + uTitStr : uTitStr;
	return title;
}

goodsCtrl.changeSpecUnitForPhoto = function (type) {
	if (!this.isSys) return; //非爱搜不调用
	let that = this;
	let len = this.gdPhotoArr.length;
	for (let i = 0; i < len; i++) {
		let sku = this.gdPhotoArr[i];

		let arr = String(sku.skuIds).split(",");
		let res = getSpecUnitIdsTitle(arr);
		sku.skuIds = res.skuIds;
		sku.skuTitles = res.titles;
	}

	function getSpecUnitIdsTitle(arr) {
		let skuIds = "";
		let titles = "";
		for (let i = 0; i < arr.length; i++) {
			let data = {};
			let t = String(arr[i]).split("_");
			if (type == 2) {
				data = that.checkSpecTitle(t[1], t[0]);
			} else {
				if (t.length == 2) {
					data = that.checkSpecTitle(t[1], t[0]);
				} else {
					data = that.checkSpecTitle(t[0]);
				}
			}

			if (data) {
				if (skuIds == "") {
					skuIds = data.ids;
				} else {
					skuIds = skuIds + "," + data.ids;
				}

				if (titles == "") {
					titles = data.titles;
				} else {
					titles = titles + "," + data.titles;
				}
			}
		}
		let res = {
			skuIds: skuIds,
			titles: titles
		};
		return res;
	}
}

goodsCtrl.setSaveGdSUPhoto = function () {
		let that = this;
		let len = this.gdPhotoArr.length;
		let imgLs = [];
		for (let i = 0; i < len; i++) {
			let sku = this.gdPhotoArr[i];
			if (!sku.imgs || !sku.imgs_base) continue;
			let data = {};
			data.img = sku.imgs_base;
			if (i == 0)
				data.type = 1;
			else
				data.type = 0;
			if (sku.skuIds) {
				let arr = String(sku.skuIds).split(",");
				let res = getSpecUnitIdsTitle(arr);
				data.spec_unit_ids = res.spec_unit_ids;
				data.spec_unit_txt = res.spec_unit_txt;
			} else {
				data.spec_unit_ids = [];
				data.spec_unit_txt = [];
			}
			imgLs.push(data);
		}
		return imgLs;

		function getSpecUnitIdsTitle(arr) {
			let spec_unit_ids = [];
			let spec_unit_txt = [];
			for (let i = 0; i < arr.length; i++) {
				let data = null;
				let t = String(arr[i]).split("_");
				if (t.length == 2) //规矩 _单位
				{
					data = that.checkSpecTitle(t[1], t[0]);
				} else //单位
				{
					data = that.checkSpecTitle(t[0]);
				}

				if (data) {
					spec_unit_ids.push(data.ids);
					spec_unit_txt.push(data.titles);
				}
			}

			let res = {};
			res.spec_unit_ids = spec_unit_ids;
			res.spec_unit_txt = spec_unit_txt;
			return res;
		}
	},

	goodsCtrl.checkSpecTitle = function (uId, sId) {
		let s = this.getCheckedSpecArr();
		let u = this.getSelectedUnit();
		let data = {};
		if (sId) {
			for (let i = 0; i < s.length; i++) {
				for (let j = 0; j < u.length; j++) {
					if (s[i].ids.indexOf(sId) && u[j].id == uId) {
						data.ids = sId + "_" + u[j].id;
						data.titles = s[i].titles + "," + u[j].title;
						return data;
					}
				}
			}
		} else {
			for (let i = 0; i < u.length; i++) {
				if (u[i].id == uId) {
					data.ids = u[i].id.id;
					data.titles = u[i].title;
					return data;
				}
			}
		}
		return data;
	}


/*****保存产品*******/
goodsCtrl.saveGoods = function (isShowGgInfo = true) {
	if (goodsTlpCtrl.getTplTitleLimit() && this.saveGB.title == "") {
		that.$msg({
			title: lang.savaGoodTxt.noTitle
		});
		return;
	}
	if (goodsTlpCtrl.getTplItemNoLimit() && this.saveGB.item_no == "") {
		that.$msg({
			title: lang.savaGoodTxt.noGoodsNo
		});
		return;
	}
	this.saveGB.spec_ls = this.saveSpec(); //规格

	let priceQuoteType = goodsTlpCtrl.getPriceQuoteType();
	this.saveGB.costs = this.saveCosts(); //成本
	let inputCost = false;
	if (this.saveGB.costs && this.saveGB.costs.length > 0) {
		for (let i = 0; i < this.saveGB.costs.length; i++) {
			if (this.saveGB.costs[i].cost > 0) {
				inputCost = true;
			}
		}
	}
	if ((priceQuoteType == 2 && !this.saveGB.spec_ls && inputCost) //按规格报价 填写成本提示要选择一个规格
		||
		(priceQuoteType == 2 && this.saveGB.spec_ls.length == 0 && inputCost)) {
		that.$msg({
			title: lang.savaGoodTxt.noSpecTip
		});
		return;
	}


	this.saveGB.prices = this.savePrice(); //价格
	this.saveGB.qty = this.savaQty();

	let param = {};
	let boolnum = 0
	let self = this;
	param = this.saveGB;
	if (param.qty && param.qty.qty.length > 0) {
		param.qty.qty.map(v => {
			v.list.map(vs => {
				vs.list.map(vw => {
					if (vw.num) {
						boolnum++
					}
				})
			})
		})
	}
	if (!param.qty || boolnum == 0) {
		delete param.qty
	} else {
		param.qty = JSON.stringify(param.qty)
	}
	if (this.saveGB.goods_id == "") //添加产品
	{
		this.saveGB.unit_ls = this.saveUnit(); //单位
		this.saveGB.brands = this.saveBrandsTags(this.brandList); //品牌
		this.saveGB.tags = this.saveBrandsTags(this.tagList);
		this.saveGB.desc = this.saveMoreDetail();

		if (this.isSys)
			this.saveGB.img_ls = this.setSaveGdSUPhoto(); //爱搜专用
		https.saveAddGds(param, callBack); //新增产品
	} else {
		//编辑产品
		this.saveGB.unit_ls = JSON.stringify(this.saveUnit()); //单位
		this.saveGB.brands = JSON.stringify(this.saveBrandsTags(this.brandList)); //品牌
		this.saveGB.tags = JSON.stringify(this.saveBrandsTags(this.tagList));
		this.saveGB.desc = JSON.stringify(this.saveMoreDetail());

		if (this.isSys)
			this.saveGB.img_ls = this.setSaveGdSUPhoto(); //爱搜专用

		param.unitList = JSON.parse(this.saveGB.unit_ls);
		param.spec_ckls = this.saveGB.spec_ls;
		https.uploadsGoods(param, callBack); //编辑产品
	}

	function callBack(data) {
		if (data) {
			let obj = {};
			obj.id = data.goods_id;
			obj.act = data.act;
			obj.isShowGgInfo = isShowGgInfo;
			if (self.actType == staticConst.ACT_GOODS_DERIFT) //草稿提交服务器保持完成清除草稿
			{
				self.getDerift();
			}
			uni.$emit(eventCtrl.ADD_GOODS_EVENT_SUCCESS, obj);

		}
	}
}

/***编辑产品设置提交的产品id****/
goodsCtrl.setGoodsId = function (id) {
	this.saveGB.goods_id = id;
}

/****获取提交选中的规格数据*****/
goodsCtrl.saveSpec = function () {
	let arr = this.specList;
	let len = arr.length;
	let tempArr = [];
	for (let i = 0; i < len; i++) {
		let o = arr[i];
		if (o.checked) {
			if (o.son) {
				let arr1 = o.son;
				let isSelect1 = false;
				for (let j = 0; j < arr1.length; j++) {
					let oo = arr1[j]
					if (oo.checked) {
						isSelect1 = true;
						if (oo.son) {
							let arr2 = oo.son;
							let isSelect = false;
							for (let k = 0; k < arr2.length; k++) {
								let ooo = arr2[k];
								if (ooo.checked) {
									isSelect = true;
									tempArr.push(ooo.id);
								}
							}
							if (!isSelect) {
								tempArr.push(oo.id);
							}
						} else {
							tempArr.push(oo.id);
						}
					}
				}
				if (!isSelect1) {
					tempArr.push(o.id);
				}
			} else {
				tempArr.push(o.id);
			}
		}
	}
	return tempArr;
}

/****按规格报价，输入价格是提示先选规格****/
goodsCtrl.checkPriceInputSpec = function () {
	let arr = this.saveSpec();
	if (arr.length <= 0) {
		that.$msg({
			title: lang.tipSelectSpecTxt
		});
		return false;
	}
	return true;
}


/*****获取提交单位列表*****/
goodsCtrl.saveUnit = function () {
	let uObj = {};
	uObj.coversion = {};
	uObj.unitids = [];
	let coversioninfo = [];
	let minunit = "";
	let minunit_name = "";

	for (let k in this.mapUnitCover) {
		let obj = {};
		obj.covernum = this.mapUnitCover[k].covernum;
		obj.id = this.mapUnitCover[k].id;
		obj.unitName = this.mapUnitCover[k].name_rel;
		obj.name_rel = this.mapUnitCover[k].name_rel;
		if (this.mapUnitCover[k].isMin) {
			minunit = this.mapUnitCover[k].id;
			minunit_name = this.mapUnitCover[k].name_rel;
		}
		uObj.unitids.push(obj.id);
		coversioninfo.push(obj);
	}
	uObj.coversion.coversioninfo = coversioninfo;
	uObj.coversion.minunit = minunit;
	uObj.coversion.minunit_name = minunit_name;
	uObj.coversion.minunitname = minunit_name;
	return uObj;
}

/*******获取提交的价格******/
goodsCtrl.savePrice = function () {
	let tempArr = [];
	let map = this.mapPrice;
	let key = "";
	let obj;
	let ordered = {};
	Object.keys(map).sort().forEach(function (key) {
		ordered[key] = map[key];
	}); //排序
	for (let k in ordered) {
		if (String(k).indexOf('pricec') >= 0) continue;
		let price = ordered[k].price;
		let money_q = ordered[k].money_q;
		let arr = String(k).split("|");
		let arr1 = String(arr[arr.length - 1]).split("_");
		if (!obj || obj.specid != arr1[0] || obj.unitid != arr1[1]) {
			if (obj)
				tempArr.push(obj); //保存
			obj = new Object();
			obj.specid = arr1[0];
			obj.unitid = arr1[1];
			obj.priceinfo = [];
		}
		let pObj = {};
		pObj.price = price;
		pObj.money_q = money_q;
		pObj.priceid = staticConst.PRICE_STR + arr1[2];
		let tArr = String(k).split("_");
		if (ordered[tArr[0] + "_" + tArr[1] + "_" + 'pricec']) {
			obj.pricec = ordered[tArr[0] + "_" + tArr[1] + "_" + 'pricec'].price;
		} else {
			obj.pricec = 0
		}
		obj.priceinfo.push(pObj);
	}
	tempArr.push(obj); //最后一个添加
	return tempArr;
}

/*****获取提交成本******/
goodsCtrl.saveCosts = function () {
	let map = this.mapCost;
	let tempArr = [];
	for (let k in map) {
		let cost = map[k];
		let arr = String(k).split("_");
		let obj = {};
		obj.cost = cost;
		let arrSp = String(arr[0]).split("|");
		obj.specid = arrSp[arrSp.length - 1];
		obj.unitid = arr[1];
		tempArr.push(obj);
	}
	return tempArr;
}

/****保存品牌,标签*****/
goodsCtrl.saveBrandsTags = function (arr) {
	let tempArr = [];
	if (arr) {
		let len = arr.length;
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
			if (obj.checked) {
				tempArr.push(obj.id);
			}
		}
	}
	return tempArr;
}

/****保存产品详情数据*****/
goodsCtrl.saveMoreDetail = function () {
	let tempArr = [];
	let len = this.moreGdDetail.length;
	for (let i = 0; i < len; i++) {
		let obj = this.moreGdDetail[i];
		let data = {};
		data.id = obj.id;
		data.desc = {};
		data.desc.list = [];
		data.desc.title = obj.title;
		let list = obj.list;
		for (let j = 0; j < list.length; j++) {
			let d1 = {};
			let o = list[j];
			d1.desc = o.desc;
			let list1 = o.list;
			for (let k = 0; k < list1.length; k++) {
				let ooo = list1[k];
				d1.img = ooo.imgs; //注意服务
			}

			if (d1.desc != "" || d1.img != "") {
				data.desc.list.push(d1);
			}
		}
		tempArr.push(data);
	}
	return tempArr;
}

/******新增产品保存初始库存*******/
goodsCtrl.savaQty = function () {
	let qty = [];
	let map = this.mapQty;
	for (let k in map) {
		let num = map[k];
		let arr = String(k).split("_");
		let storeId = arr[0];
		let unitId = arr[2];
		let arr1 = String(arr[1]).split("|");
		let specId = arr1[arr1.length - 1];
		let obj = {};
		obj.unitid = unitId;
		obj.num = num;
		insertData(storeId, specId, obj);
	}

	let qtyObj = {};
	if (qty.length > 0) {
		qtyObj.qty = qty;
	} else {
		qtyObj = null;
	}
	return qtyObj;

	function insertData(storeId, specId, data) {
		let len = qty.length;
		let hasStore = false;
		for (let i = 0; i < len; i++) {
			let o = qty[i];
			if (o.storeid == storeId) {
				hasStore = true;
				let arr = o.list;
				let hasSpec = false;
				for (let j = 0; j < arr.length; j++) {
					let oo = arr[j];
					if (oo.specid == specId) {
						hasSpec = true;
						oo.list.push(data);
					}
				}
				if (!hasSpec) {
					let spObj = {};
					spObj.specid = specId;
					spObj.list = [];
					spObj.list.push(data);
					o.list.push(spObj);
				}
			}
		}
		if (!hasStore) {
			let spObj = {};
			spObj.specid = specId;
			spObj.list = [];
			spObj.list.push(data);
			let sObj = {};
			sObj.storeid = storeId;
			sObj.list = [];
			sObj.list.push(spObj);
			qty.push(sObj);
		}
	}
}


/******产品图片******BEGIN***/
goodsCtrl.getGdsPhotos = async function () {
		let data = this.getGoodsInfoBean();
		let cp = await localStore.getCpInfo();
		this.isSys = cp.is_sys == "1" ? true : false;
		this.gdPhotoArr = [];
		if (data) {
			if (!this.isSys && data.imgs_s) {
				let len = data.imgs_s.length;
				for (let i = 0; i < len; i++) {
					let o = {};
					o.url = data.imgs_s[i];
					o.imgs_base = data.imgs_base[i];
					o.imgs = data.imgs[i];
					o.index = i;
					o.id = i;
					o.isDel = false;
					this.gdPhotoArr.push(o);
				}
			}

			if (this.isSys && data.img_ls) {
				let len = data.img_ls.length;
				for (let i = 0; i < len; i++) {
					let temp = data.img_ls[i];
					let o = {};
					o.url = temp.img;
					o.imgs_base = temp.img_base;
					o.imgs = temp.img;
					o.index = i;
					o.id = i;
					o.isDel = false;

					o.skuIds = "";
					if (temp.spec_unit_ids && temp.spec_unit_ids.length > 0) {

						let arr = temp.spec_unit_ids;
						let ary = [];
						arr.forEach(s => {
							if (s != "0_0") {
								if (o.skuIds == "") {
									o.skuIds = s;
								} else {
									o.skuIds = o.skuIds + "," + s;
								}
								ary.push(s);
							}
						});
					}

					o.skuTitles = "";
					if (temp.spec_unit_txt && temp.spec_unit_txt.length > 0) {
						let ary = [];
						temp.spec_unit_txt.forEach(s => {
							s = s.replace(/,$|，$/gi, "");
							ary.push(s);
						});
						o.skuTitles = ary.join("");
					}
					this.gdPhotoArr.push(o);
				}
			}
		}
		if (this.gdPhotoArr.length < 9)
			this.gdPhotoArr.push({
				url: "",
				imgs_base: "",
				imgs: "",
				isDel: false,
				skuIds: "",
				skuTitles: ""
			});
		return this.gdPhotoArr;
	},

	/****删除图片****/
	goodsCtrl.delGdsPhoto = function (index) {
		let len = this.gdPhotoArr.length;
		if (this.gdPhotoArr[len - 1].url != "")
			this.gdPhotoArr.push({
				url: "",
				imgs_base: "",
				imgs: "",
				isDel: false,
				id: 0,
				skuIds: "",
				skuTitles: ""
			});
		this.gdPhotoArr.splice(index, 1);
		this.saveGB.imgs.splice(index, 1);
		if (index == 0) {
			if (this.saveGB.imgs[index])
				this.saveGB.imgs[index].type = 1;
		}
		return this.gdPhotoArr;
	}

/***交换图片****/
goodsCtrl.switchPhoto = function (arr) {
	this.gdPhotoArr = arr;
	this.saveGB.imgs = [];
	for (let i = 0; i < this.gdPhotoArr.length; i++) {
		let obj = this.gdPhotoArr[i];
		let img = {};
		img.type = 0;
		if (i == 0)
			img.type = 1;
		img.image = obj.imgs_base;
		this.saveGB.imgs.push(img);
	}
}

/****上传服务器的路径数组****/
goodsCtrl.uploadGdsPhoto = function (index, res) {
	let obj = {};
	if (index == 0) {
		obj.type = 1;
	} else {
		obj.type = 0;
	}
	obj.image = res.url;
	this.gdPhotoArr[index].imgs = res.url;
	this.gdPhotoArr[index].imgs_base = res.name;
	this.gdPhotoArr[index].url = res.url;
	this.saveGB.imgs[index] = obj;
}
/******产品图片******END***/

/****产品名称****/
goodsCtrl.setGoodsName = function (value) {
	this.saveGB.title = String(value).trim();
}

/******产品货号********/
goodsCtrl.setGoodsNo = function (value) {
	this.saveGB.item_no = String(value).trim();
}

/****产品条码****/
goodsCtrl.setGoodsBarCode = function (value) {
	this.saveGB.barcode = String(value).trim();
}

/***获取编辑产品详情***/
goodsCtrl.gdInfoBean = function () {
	return this.goodsInfoBean;
}

/********规格*****BEGIN****/
/***规格数据返回****/
goodsCtrl.specDataBack = function (data) {
	if (data != null) {
		if (!goodsCtrl.specList) {
			goodsCtrl.specList = [];
			goodsCtrl.specList = goodsCtrl.specList.concat(data);
		} else {
			goodsCtrl.specList = goodsCtrl.specList.concat(data) //data;
		}
		goodsCtrl.specList = goodsCtrl.searSeSpceArr.concat(goodsCtrl.specList); //specList.concat(data)
		goodsCtrl.specList = util.removeDuplicate(goodsCtrl.specList, 'id');
		goodsCtrl.initSpecData();
	}
}

/****加载更多规格*****/
goodsCtrl.loadMoreSpec = async function (back = null, kw = "") {
		let params = {};
		let isChangeKw = false;
		if (this.kwSpec == kw) {
			this.specLsNo++;
		} else {
			this.specLsNo = 0;
			isChangeKw = true;
		}
		if (kw != "") {
			params.kw = kw;
		}
		this.kwSpec = kw;
		params.status = 1;
		params.pfrom = this.specLsNo * PAGE_SIZE;
		if (this.goodsInfoBean)
			params.gdid = this.goodsInfoBean.specgd_id ? this.goodsInfoBean.specgd_id : this.goodsInfoBean.id;
		let res = await https.loadSpecList(params);

		if (res && isChangeKw) {
			this.specList = [];
		}

		if (res) {
			this.specDataBack(res);
		} else {
			if (kw != "") {
				if (params.pfrom == 0) {
					that.$msg({
						title: "没有找到分类包含 '" + kw + "' 数据!"
					});
				} else {
					that.$msg({
						title: "没有找到更多分类包含 '" + kw + "' 数据!"
					});
				}
			} else {
				that.$msg({
					title: "没有更多的分类数据!"
				});
			}
		}
		if (back) {
			back();
			back = null;
		}
	},

	/****加载更多品牌****/
	goodsCtrl.loadMoreBrand = async function (back = null, kw = "") {
			let params = {};
			if (this.kwBrand == kw) {
				this.barndNo++;
			} else {
				this.barndNo = 0;
			}

			if (kw != "")
				params.kw = kw
			this.kwBrand = kw;
			params.status = 1;
			params.pfrom = this.barndNo * PAGE_SIZE * 2;
			if (this.goodsInfoBean)
				params.gdid = this.goodsInfoBean.id;
			let res = await https.loadBrandList(params);

			if (res && params.pfrom == 0) {
				this.brandList = [];
			}

			if (res) {
				this.brandBack(res);
			} else {
				if (kw != "") {
					if (params.pfrom == 0) {
						that.$msg({
							title: "没有找到品牌包含 '" + kw + "' 数据!"
						});
					} else {
						that.$msg({
							title: "没有找到更多品牌包含 '" + kw + "' 数据!"
						});
					}
				} else {
					that.$msg({
						title: "没有更多的品牌数据!"
					});
				}
			}

			if (back) {
				back();
				back = null;
			}
		},

		/****选中的规格****/
		goodsCtrl.initSpecData = function () {
			this.seleFirstArr = [];
			this.seleSecondArr = [];
			if (this.goodsInfoBean == null) return;
			let that = this;
			let i, len, arr = this.goodsInfoBean.spec_ckls;
			len = arr ? arr.length : 0;

			if (len > 0) {
				for (i = 0; i < len; i++) {
					dataHanler(arr[i]);
				}
			} else {
				dataHanler(-999);
			}

			function dataHanler(id) {
				if (that.specList) {
					for (let j = 0; j < that.specList.length; j++) {
						let o = that.specList[j];
						o.index = j;
						if (!o.checked) {
							if (o.id == id) {
								o.checked = true;
								checked(o, 0);
							} else {
								o.checked = false;
							}
						}

						if (o.son) {
							let oList = o.son;
							for (let k = 0; k < oList.length; k++) {
								let oo = oList[k];
								oo.index = k;
								if (!oo.checked) {
									if (oo.id == id) {
										oo.checked = true;
										o.checked = true;
										checked(o, 0);
										checked(oo, 1);
									} else {
										oo.checked = false;
									}
								}

								if (oo.son) {
									let ooList = oo.son;
									for (let n = 0; n < ooList.length; n++) {
										let ooo = ooList[n];
										ooo.index = n;
										if (!ooo.checked) {
											if (ooo.id == id) {
												ooo.checked = true;
												oo.checked = true;
												o.checked = true;
												checked(o, 0);
												checked(oo, 1);
												checked(ooo, 2);
											} else {
												ooo.checked = false;
											}
										}
									}
								}
							}
						}
					}
				}
			}

			function checked(obj, level) {
				let data = {};
				data.level = level;
				data.data = obj;
				that.selectHandler(data);
			}
		}

/***关闭/选中规格***/
goodsCtrl.selectHandler = function (data) {
	let spec = goodsTlpCtrl.getTplSpecLimit();
	let that = this;
	let level = data.level;
	let obj = {};
	obj.id = data.data.id;
	obj.title = data.data.title;
	let checked = obj.checked = data.data.checked;
	obj.index = data.data.index;
	obj.isFirstLevel = false;
	let res = {};
	let ary = [];
	if (level == 0) //一级
	{
		if (!spec) {
			allUncheckedFisrt(obj.id);
			this.seleFirstArr = [];
		}
		this.seleSecondArr = [];

		let objData = this.getSecList(obj.id);

		res.list = objData.son;
		ary = this.seleFirstArr;
		scanfSearSeSpec(checked, objData);
	} else if (level == 1) //二级
	{
		res.list = this.getThdList(obj.id);
		ary = this.seleSecondArr;
	}
	obj.hasSon = res.list ? true : false;
	if (checked) {
		res.titleList = addTitleArrEle(ary, obj);
	} else {
		res.titleList = revTitleArrEle(ary, obj);
	}
	if (res.titleList && res.titleList.length > 0)
		res.titleList = util.removeDuplicate(res.titleList, 'id');

	res.hasSon = checkHasSon(res.titleList);

	return res;

	/******未开多规格是时候，只能选中一个规格********/
	function allUncheckedFisrt(id) {
		if (that.specList) {
			for (let i = 0; i < that.specList.length; i++) {
				let obj = that.specList[i];
				if (obj.id == id) continue;
				obj.checked = false;
			}
		}
	}

	/****检测是否有子级****/
	function checkHasSon(arr) {
		let len = arr.length;
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
			let has = that.checkHasSon(obj.id);
			if (has) {
				return true;
			}
		}
		return false;

	}

	//添加title到列表元素，
	function addTitleArrEle(arr, item) //添加，
	{
		let len = arr.length;
		if (len == 0) {
			arr.push(item);
		} else {
			let posIdx = -1;
			for (let i = 0; i < len; i++) {
				let obj = arr[i];
				if (posIdx == -1 && obj.index > item.index) {
					posIdx = i; //插入位置
				}
				obj.checked = false;
			}
			if (posIdx == -1) {
				arr.push(item);
			} else {
				arr.splice(posIdx, 0, item); //插入位置
			}
		}
		return arr;
	}

	//删除tilte列表元素
	function revTitleArrEle(arr, item) //删除
	{
		let len = arr.length;
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
			if (obj.id == item.id) {
				arr.splice(i, 1);
				break;
			}
		}
		return arr;
	}


	function scanfSearSeSpec(ch, data) {
		let idx = -1;
		if (that.searSeSpceArr) {
			for (let i = 0; i < that.searSeSpceArr.length; i++) {
				let obj = that.searSeSpceArr[i];
				if (obj.id == data.id) {
					idx = i;
					break;
				}
			}
			if (ch && idx == -1) {
				that.searSeSpceArr.push(data);
			} else {
				if (idx <= that.searSeSpceArr.length - 1)
					that.searSeSpceArr.splice(idx, 1);
			}
		}

	}
}

/***获取一级选中规格***/
goodsCtrl.getInitFirstSeleData = function () {
	return this.seleFirstArr;
}

/***获取二级选中规格***/
goodsCtrl.getInitSeleSeleData = function () {
	return this.seleSecondArr;
}

/***获取规格列表数据***/
goodsCtrl.getFirstList = function () {
	return this.specList;
}

/***二级规格***/
goodsCtrl.getSecList = function (id) {
	let i, len = this.specList.length;
	for (i = 0; i < len; i++) {
		let obj = this.specList[i]
		if (id == obj.id) {
			return obj;
		}
	}
}

/****三级规格****/
goodsCtrl.getThdList = function (id) {
	let i, len = this.specList.length;
	for (i = 0; i < len; i++) {
		let o = this.specList[i];
		if (o.son) {
			let oo = o.son;
			for (let j = 0; j < oo.length; j++) {
				let ooo = oo[j];
				if (id == ooo.id) {
					return ooo.son;
				}
			}
		}
	}
}

/****检测是否有子节点，有一个就返回****/
goodsCtrl.checkHasSon = function (id) {
	let i, len = this.specList.length;
	for (i = 0; i < len; i++) {
		let o = this.specList[i];
		if (o.id == id && o.son) {
			return true;
		} else {
			if (o.son) {
				for (let j = 0; j < o.son.length; j++) {
					let oo = o.son[j];
					if (oo.id == id && oo.son) {
						return true;
					}
				}
			}
		}
	}
	return false;
}

/***选择规规格****/
goodsCtrl.onClickTab = function (data) {
	let level = data.level;
	let obj = {};
	obj.id = data.data.id;
	obj.title = data.data.title;
	obj.index = data.data.index;
	obj.isFirstLevel = false;

	let res = {};
	let ary = [];
	if (level == 1) {
		this.seleSecondArr = [];
		let objData = this.getSecList(obj.id);
		res.list = objData.son;
		ary = this.seleFirstArr;
	} else {
		res.list = this.getThdList(obj.id);
		ary = this.seleSecondArr;
	}
	obj.hasSon = res.list == undefined ? false : true;
	res.titleList = this.selectTab(ary, obj);
	if (res.titleList && res.titleList.length > 0)
		res.titleList = util.removeDuplicate(res.titleList, 'id');
	return res;
}

goodsCtrl.onClickItem = function (data) {
	let spec = goodsTlpCtrl.getTplSpecLimit();
	let level = data.level;
	let obj = {};
	obj.id = data.data.id;
	obj.title = data.data.title;
	obj.index = data.data.index;
	obj.isFirstLevel = false;
	let res = {};
	let ary = [];
	if (level == 0) {
		let objData = this.getSecList(obj.id);
		res.list = objData.son;
		ary = this.seleFirstArr;
	} else {
		res.list = this.getThdList(obj.id);
		ary = this.seleSecondArr;
	}
	obj.hasSon = res.list == undefined || res.list == null ? false : true;
	res.titleList = this.selectTab(ary, obj);
	if (res.titleList && res.titleList.length > 0)
		res.titleList = util.removeDuplicate(res.titleList, 'id');

	return res;
}

goodsCtrl.selectTab = function (arr, item) {
	let len = arr.length;
	for (let i = 0; i < len; i++) {
		let obj = arr[i];
		if (obj.id == item.id) {
			obj.checked = true;
		} else {
			obj.checked = false;
		}
	}
	return arr;
}
/********规格*****END****/

/******单位**BEGIN*****/
/****加载单位返回***/
goodsCtrl.unitBack = function (data) {
	if (data != null) {
		goodsCtrl.unitList = data;
		goodsCtrl.initSeleUnit();
	}
}

/****返回单位列表*****/
goodsCtrl.getUnitList = function () {
	return this.unitList;
}

/***设置默认初始数据***/
goodsCtrl.initSeleUnit = function () {
	let that = this;
	let len = this.unitList.length;

	for (let i = 0; i < len; i++) {
		let obj = this.unitList[i];
		let cObj = initSeleUnitList(obj.id);
		if (cObj) {
			obj.checked = true;
			this.unitCoverArr.push(cObj);
			this.mapUnitCover[cObj.id] = cObj;
			this.selectedUnitArr.push(obj);
		}
	}
	//初始化选择的单位列表
	function initSeleUnitList(id) {
		let gdBean = that.goodsInfoBean;
		if (gdBean && gdBean.unit_ls) {
			let gc = gdBean.unit_ls;
			if (gc.coversion && gc.coversion.coversioninfo) {
				let arr = gc.coversion.coversioninfo;
				let len = arr.length;
				for (let i = 0; i < len; i++) {
					let obj = arr[i];
					if (obj.id == id) {
						if (gc.coversion.minunit == obj.id) {
							obj.isMin = true;
							that.minUnit = obj;
						} else {
							obj.isMin = false;
						}
						return obj;
					}
				}
				return null;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
}

/**选中和删除单位***/
goodsCtrl.setSelectedUnit = function (item) {
	let checked = item.checked;
	let id = item.id;

	if (checked) //  删除
	{
		checkAndsubUnit(this.selectedUnitArr, id, checked);
		checkAndsubUnit(this.unitCoverArr, id, checked);
		delete this.mapUnitCover[id];
		let isHas = checkIsHasMinUnit(this.unitCoverArr);
		if (!isHas) {

			let len = this.unitCoverArr.length;
			if (len > 0) {
				let obj = this.unitCoverArr[0];
				obj.isMin = true;
				this.mapUnitCover[obj.id].isMin = true;
				this.minUnit = obj;
			}
		}
		return true;
	} else {
		let unit = goodsTlpCtrl.getTplUnitLimit(); //有无开多单位
		let len = this.selectedUnitArr.length;
		if (unit && len >= staticConst.MAX_UNIT) {
			that.$msg({
				title: lang.unitTxt.moreThanMax
			});
			return false;
		}
		let isHas = checkAndsubUnit(this.selectedUnitArr, id, checked)
		if (isHas) //有不添加
		{
			return false;
		}
		if (unit)
			this.selectedUnitArr.push(item);
		else
			this.selectedUnitArr[0] = item;
		isHas = checkAndsubUnit(this.unitCoverArr, id, checked);
		if (isHas) return; //有就不添加
		isHas = checkIsHasMinUnit(this.unitCoverArr);
		let obj = {};
		obj.covernum = 1;
		obj.id = id;
		obj.name = "",
			obj.isMin = isHas ? false : true;
		obj.name_rel = item.title;
		if (obj.isMin)
			this.minUnit = obj;
		if (unit)
			this.unitCoverArr.push(obj);
		else
			this.unitCoverArr[0] = obj;
		this.mapUnitCover[obj.id] = obj;
		return true;
	}

	function checkAndsubUnit(arr, id, checked) {
		let len = arr.length;
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
			if (obj.id == id) {
				if (checked) {
					arr.splice(i, 1);
				}
				return true;
			}
		}
		return false;
	}

	function checkIsHasMinUnit(arr) {
		let len = arr.length;
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
			if (obj.isMin) return true;
		}
		return false;
	}

}

/***修改换算列表最小单位****/
goodsCtrl.setUnitCoverArrIsMin = function (id) {
	let len = this.unitCoverArr.length;
	for (let i = 0; i < len; i++) {
		let obj = this.unitCoverArr[i];
		if (obj.id == id) {
			obj.isMin = true;
			this.mapUnitCover[obj.id].isMin = true;
		} else {
			obj.isMin = false;
			this.mapUnitCover[obj.id].isMin = false;
		}
	}
}

/****改变单位换算值****/
goodsCtrl.changUnitCover = function (data) {
		let len = this.unitCoverArr.length;
		for (let i = 0; i < len; i++) {
			let obj = this.unitCoverArr[i];
			if (obj.id == data.id) {
				obj.covernum = data.num;
				break;
			}
		}
		this.mapUnitCover[data.id].covernum = data.num;
	},

	/***获取选中的单位列表***/
	goodsCtrl.getSelectedUnit = function () {
		return this.selectedUnitArr;
	}
/***获取单位换算列表***/
goodsCtrl.getUnitCover = function () {
	return this.unitCoverArr;
}

/***设置最小单位****/
goodsCtrl.setMinUnit = function (value) {
	this.minUnit = value;
}
/********单位**END**********/


/***************价格*********BEGIN****/
goodsCtrl.getPriceList = function () {
	return this.priceList;
}

/****初始化价格****/
goodsCtrl.setSpecUnitPrice = function () {
	let that = this;
	this.mapPrice = {};
	let priceQuoteType = goodsTlpCtrl.getPriceQuoteType();
	if (priceQuoteType == 0) {
		this.priceList.forEach(p => {
			// 售价
			this.mapPrice["0_" + "0_" + p.psort] = getPrice(0, 0, p.psort);
			let hasPricec = goodsTlpCtrl.hasPricec() // 是否有划线价格
			if (hasPricec)
				this.mapPrice["0_" + "0_pricec"] = getPricec(0, 0);
		});
	} else {
		this.checkedSpecArr.forEach(s => {
			let arr = String(s.ids).split("|");
			let id = arr[arr.length - 1];
			this.selectedUnitArr.forEach(u => {
				// 成本
				// this.mapCost[s.ids + "_" + u.id] = getCost(id,u.id);
				this.priceList.forEach(p => {
					// 售价
					this.mapPrice[s.ids + "_" + u.id + "_" + p.psort] = getPrice(id, u.id, p.psort);
				});

				let hasPricec = goodsTlpCtrl.hasPricec() // 是否有划线价格
				if (hasPricec)
					this.mapPrice[s.ids + "_" + u.id + "_pricec"] = getPricec(id, u.id);
			})
		});
	}
	return this.mapPrice;

	/***获取价格***/
	function getPrice(sid, uid, psort) {
		let pObj = {
			price: 0,
			money_q: 0
		};
		if (that.goodsInfoBean) {
			let arr = that.goodsInfoBean.priceinfo;
			if (arr) {
				let len = arr.length;
				for (let i = 0; i < len; i++) {
					let p = arr[i];
					if (p && sid == p.specid && uid == p.unitid) {
						let ary = p.priceinfo;
						let leng = ary.length;
						for (let j = 0; j < leng; j++) {
							let s = ary[j];
							if (s.priceid == staticConst.PRICE_STR + psort) {
								pObj.price = s.price;
								pObj.money_q = s.money_q;
								break;
							}
						}
					}
				}
			}
		}
		return pObj;
	}

	/******划线价 */
	function getPricec(sid, uid) {
		let pObj = {
			price: 0,
			money_q: 0
		};
		if (that.goodsInfoBean) {
			let arr = that.goodsInfoBean.priceinfo;
			if (arr) {
				let len = arr.length;
				for (let i = 0; i < len; i++) {
					let p = arr[i];
					if (p && sid == p.specid && uid == p.unitid) {
						pObj.price = p.pricec;
						break;
					}
				}
			}
		}
		return pObj;
	}
}

/****价格设置，规格显示遍历****/
goodsCtrl.getPriSpec = function () {
	let priceQuoteType = goodsTlpCtrl.getPriceQuoteType();
	this.checkedSpecArr = [];
	if (priceQuoteType == 2) //按规格报价
	{
		let len = this.specList.length;
		for (let i = 0; i < len; i++) {
			let obj = this.specList[i];
			if (obj.checked) {
				if (obj.son) {
					let arr = obj.son;
					let has = checked(arr); //检查子节点是否有选中的规格
					if (has) {
						for (let j = 0; j < arr.length; j++) {
							let obj1 = arr[j];
							if (obj1.checked) {
								if (obj1.son) {
									let arr1 = obj1.son;
									let has1 = checked(arr1); //检查子节点是否有选中的规格
									if (has1) {
										for (let k = 0; k < arr1.length; k++) {
											let obj2 = arr1[k];
											if (obj2.checked) {
												let o = {};
												o.titles = obj.title + ", " + obj1.title + ", " + obj2.title; //压入当前规格与父规格的title,id 到数组
												o.ids = obj.id + "|" + obj1.id + "|" + obj2.id;
												this.checkedSpecArr.push(o);
											}
										}
									} else //没有直接压入当前规格与父规格的title,id 到数组
									{
										let o = {};
										o.titles = obj.title + "," + obj1.title;
										o.ids = obj.id + "|" + obj1.id;
										this.checkedSpecArr.push(o);
									}
								} else //没有直接压入当前规格与父规格的title,id 到数组
								{
									let o = {};
									o.titles = obj.title + "," + obj1.title;
									o.ids = obj.id + "|" + obj1.id;
									this.checkedSpecArr.push(o);
								}
							}
						}
					} else //没有直接压入当前规格的title,id 到数组
					{
						let o = {};
						o.titles = obj.title;
						o.ids = obj.id;
						this.checkedSpecArr.push(o);
					}
				} else //没有直接压入当前规格的title,id 到数组
				{
					let o = {};
					o.titles = obj.title;
					o.ids = obj.id;
					this.checkedSpecArr.push(o);
				}
			}
		}
	} else if (priceQuoteType == 1 || priceQuoteType == 0) //1:按单位报价 ,0: 无关联报价
	{
		let o = {};
		o.titles = "";
		o.ids = 0;
		this.checkedSpecArr.push(o);
	}


	return this.checkedSpecArr;

	//检测是否有选中的规格
	function checked(arr) {
		for (let i = 0; i < arr.length; i++) {
			let obj = arr[i];
			if (obj.checked) {
				return true;
			}
		}
		return false;
	}
}

/****安装报价方式，获取获取单位列表***/
goodsCtrl.getPriceUnit = function () {
	let priceQuoteType = goodsTlpCtrl.getPriceQuoteType();
	if (priceQuoteType == 0) {
		let arr = [];
		let obj = {};
		obj.id = 0;
		obj.title = "";
		arr.push(obj);
		return arr;
	}
	return this.selectedUnitArr;
}

/****** 获取选择中规格title id串  成本要用到*****/
goodsCtrl.getCheckedSpecArr = function () {
	return this.checkedSpecArr;
}

/****改变计算价格***/
goodsCtrl.changePriceCover = function (data) {
	let id = data.id
	if (data.type == 1) {
		this.mapPrice[id].price = data.num; //.toFixed(2);
	} else {
		this.mapPrice[id].money_q = data.num; //.toFixed(2);
	}
}

goodsCtrl.getMapPrice = function () {
	return this.mapPrice;
}

/****快速填充价格****/
goodsCtrl.qucklyFillPrice = function (callBack, notTip = true) {
	let has = false;
	let minPrice = 0;
	let minPricec = 0;
	let minCoupons = 0;
	for (let k in this.mapPrice) //找出填充的价格
	{
		let price = Number(this.mapPrice[k].price);
		let coup = Number(this.mapPrice[k].money_q);
		if (price <= 0) continue;
		has = true;
		let id = String(k).split("_")[1];
		let temp = this.mapUnitCover[id];
		if (minPrice == 0) {
			minPrice = price / Number(temp.covernum);
			minCoupons = coup / Number(temp.covernum);
		}
		if (minPricec == 0 && String(k).indexOf('_pricec') > 0) {
			let pc = this.mapPrice[k].price;
			minPricec = pc / Number(temp.covernum);
		}
		if (minPrice != 0 && minPricec != 0) {
			break;
		}
	}

	if (notTip && !has) {
		that.$msg({
			title: "请至少输入1个价格。"
		});
		return;
	}
	for (let s in this.mapPrice) {
		let temp = this.mapPrice[s];
		let obj = {};
		obj.id = s;
		if (String(s).indexOf('_pricec') > 0) {
			let id = String(s).split("_")[1];
			let t = this.mapUnitCover[id];
			let pricec = minPricec * Number(t.covernum);
			this.mapPrice[s].price = pricec.toFixed(2);
			obj.num = pricec.toFixed(2);
			if (callBack) {
				callBack(obj);
			}
		} else {
			let id = String(s).split("_")[1];
			let t = this.mapUnitCover[id];
			let price = minPrice * Number(t.covernum);
			this.mapPrice[s].price = price.toFixed(2);
			obj.num = price.toFixed(2);
			let coupon = minCoupons * Number(t.covernum);
			this.mapPrice[s].money_q = coupon;
			obj.coupon = coupon;
			if (callBack) {
				callBack(obj);
			}
		}
	}
	callBack = null;
}

/***************价格*********END****/

/***************成本*********BEGIN****/
goodsCtrl.getCost = function () {
	return this.mapCost;
}

/****安装报价方式，获取获取单位列表***/
goodsCtrl.getCostUnit = function () {
	let costQuoteType = goodsTlpCtrl.getCostQuoteType();
	if (costQuoteType == 0) {
		let obj = {};
		obj.id = 0;
		obj.title = "";
		let arr = [];
		arr.push(obj);
		return arr;
	}
	return this.selectedUnitArr;
}

/****初始化成本****/
goodsCtrl.setSpecUnitCost = function () {
	let that = this;
	this.mapCost = {};
	this.checkCostSpecArr.forEach(s => {
		let arr = String(s.ids).split("|");
		let id = arr[arr.length - 1];
		this.selectedUnitArr.forEach(u => {
			// 成本
			this.mapCost[s.ids + "_" + u.id] = getCost(id, u.id);
		})
	});
	return this.mapCost;
	/***获取成本****/
	function getCost(sid, uid) {
		let cost = 0;
		if (that.goodsInfoBean) {
			let arr = that.goodsInfoBean.costInfo;
			if (arr) {
				let len = arr.length;
				for (let i = 0; i < len; i++) {
					let c = arr[i];
					if (c.specid == sid && c.unitid == uid) {
						cost = c.cost;
						break;
					}
				}
			}
		}
		return cost;
	}
}

/****成本设置，规格显示遍历****/
goodsCtrl.getCostSpecArr = function () {
	let costQuoteType = goodsTlpCtrl.getCostQuoteType();
	this.checkCostSpecArr = [];
	if (costQuoteType == 2) //按规格报价
	{
		let len = this.specList.length;
		for (let i = 0; i < len; i++) {
			let obj = this.specList[i];
			if (obj.checked) {
				if (obj.son) {
					let arr = obj.son;
					let has = checked(arr); //检查子节点是否有选中的规格
					if (has) {
						for (let j = 0; j < arr.length; j++) {
							let obj1 = arr[j];
							if (obj1.checked) {
								if (obj1.son) {
									let arr1 = obj1.son;
									let has1 = checked(arr1); //检查子节点是否有选中的规格
									if (has1) {
										for (let k = 0; k < arr1.length; k++) {
											let obj2 = arr1[k];
											if (obj2.checked) {
												let o = {};
												o.titles = obj.title + ", " + obj1.title + ", " + obj2.title; //压入当前规格与父规格的title,id 到数组
												o.ids = obj.id + "|" + obj1.id + "|" + obj2.id;
												this.checkCostSpecArr.push(o);
											}
										}
									} else //没有直接压入当前规格与父规格的title,id 到数组
									{
										let o = {};
										o.titles = obj.title + "," + obj1.title;
										o.ids = obj.id + "|" + obj1.id;
										this.checkCostSpecArr.push(o);
									}
								} else //没有直接压入当前规格与父规格的title,id 到数组
								{
									let o = {};
									o.titles = obj.title + "," + obj1.title;
									o.ids = obj.id + "|" + obj1.id;
									this.checkCostSpecArr.push(o);
								}
							}
						}
					} else //没有直接压入当前规格的title,id 到数组
					{
						let o = {};
						o.titles = obj.title;
						o.ids = obj.id;
						this.checkCostSpecArr.push(o);
					}
				} else //没有直接压入当前规格的title,id 到数组
				{
					let o = {};
					o.titles = obj.title;
					o.ids = obj.id;
					this.checkCostSpecArr.push(o);
				}
			}
		}
	} else if (costQuoteType == 1 || costQuoteType == 0) //1:按单位报价 ,0: 无关联报价
	{
		let o = {};
		o.titles = "";
		o.ids = 0;
		this.checkCostSpecArr.push(o);
	}

	return this.checkCostSpecArr;

	//检测是否有选中的规格
	function checked(arr) {
		for (let i = 0; i < arr.length; i++) {
			let obj = arr[i];
			if (obj.checked) {
				return true;
			}
		}
		return false;
	}
}

/***成本计算****/
goodsCtrl.changeCost = function (data) {
	this.mapCost[data.id] = Number(data.num).toFixed(2);
}

/****快速填充成本***/
goodsCtrl.qucklyFillCost = function (notTip = true) {
	let price = 0;
	for (let k in this.mapCost) //找出填充的价格
	{
		let arr = String(k).split("_");
		if (this.mapCost[k] <= 0) continue;
		let temp = this.mapUnitCover[arr[1]];
		price = this.mapCost[k];
		price = price / Number(temp.covernum);
		break;
	}
	if (notTip && price == 0) {
		that.$msg({
			title: "请至少输入1个成本。"
		});
		return;
	}
	for (let s in this.mapCost) {
		let arr = String(s).split("_");
		let temp = this.mapUnitCover[arr[1]];
		let cPrice = price * Number(temp.covernum); //Number(tempUnitPrice[k]).toFixed(2) ;
		this.mapCost[s] = cPrice.toFixed(2);
		let obj = {};
		obj.id = s;
		obj.num = cPrice.toFixed(2);
		uni.$emit(eventCtrl.CHANGE_COST_EVENT, obj);
	}
}
/***************成本*******END******/

/***加载价格列表***/
goodsCtrl.priceBack = async function (data) {
	if (data != null) {
		goodsCtrl.priceList = data;
	}
}

/***加载品牌列表***/
goodsCtrl.brandBack = async function (data) {
	let that = this;
	if (data != null) {
		if (!this.brandList) {
			this.brandList = [];
			this.brandList = goodsCtrl.brandList.concat(data);
		} else {
			this.brandList = goodsCtrl.brandList.concat(data);
		}
	}
}





/****加载分类返回*****/
goodsCtrl.catBack = async function (data) {
	if (data != null) {
		goodsCtrl.catList = data;
		goodsCtrl.initCat();
	}
}

/***加载标签返回*****/
goodsCtrl.tagBack = function (data) {
	if (data != null) {
		goodsCtrl.tagList = data;
	}
}

/***详情列表返回*****/
goodsCtrl.attrBack = function (data) {
	if (data != null) {
		goodsCtrl.attrList = data.attr_ls;
	}
}

/****仓库列表返回******/
goodsCtrl.storeBack = function (data) {
	if (data != null) {
		goodsCtrl.storeList = data;
	}
}

/****返回经手人****/
goodsCtrl.ucpBack = function (data) {
	if (data != null) {
		goodsCtrl.ucpList = data;
	}
}

/***加载数据*****/
goodsCtrl.loadConfInfo = async function () {
	let specData = await https.loadSpecList({
		status: 1,
		pfrom: this.specLsNo * PAGE_SIZE,
		pnum: PAGE_SIZE,
		gdid: this.goodsInfoBean && this.goodsInfoBean.id ? this.goodsInfoBean.id : this.goodsInfoBean && this.goodsInfoBean.specgd_id ? this.goodsInfoBean.specgd_id : ''
	}); //规格
	this.specDataBack(specData);

	await https.loadUnitList(this.unitBack); //单位
	await https.loadPriceList(this.priceBack); //价格
	let brandData = await https.loadBrandList({
		status: 1,
		pfrom: this.barndNo * PAGE_SIZE,
		gdid: this.goodsInfoBean ? this.goodsInfoBean.id : ""
	});
	this.brandBack(brandData);

	await https.loadCatList(this.catBack); //分类
	await https.loadTagList(this.tagBack); //标签
	await https.loadStoreList(this.storeBack); //仓库
	await https.loadAttrList(this.attrBack); //产品详情
	await https.loadUcpList(this.ucpBack); //经手人
}

/******编辑，添加产品******/
goodsCtrl.setData = async function (data) {
	if (!data) return;

	this.clearData();
	if (!this.saveGB)
		this.saveGB = new saveGdsBean();
	goodsTlpCtrl.setGoodsTplBean(data.tpl_info);
	this.actType = data.actType;
	//编辑产品有产品，新增产品没有产品信息
	if ((data.actType == staticConst.ACT_GOODS_EIDT ||
			data.actType == staticConst.ACT_GOODS_COPY ||
			data.actType == staticConst.ACT_GOODS_DERIFT) &&
		data.goodsInfo) {

		if (data.actType == staticConst.ACT_GOODS_COPY) //复制产品
		{
			data.goodsInfo.specgd_id = data.goodsInfo.id;
			data.goodsInfo.id = "";
			// data.goodsInfo.priceinfo = [];
			// data.goodsInfo.costInfo = [];
			this.saveGB.act = staticConst.ATCTION_GOODS_ADD; //操作：add=新增,复制新增，

			this.isEditGoods = staticConst.ACT_GOODS_COPY

		} else if (data.actType == staticConst.ACT_GOODS_DERIFT) //添加草稿产品
		{
			this.saveGB.act = staticConst.ATCTION_GOODS_ADD; //操作：add=新增,复制新增，
			if (data.goodsInfo)
				this.initDeriftMapQty(data.goodsInfo.mapQty); //草稿,读取库存数据；
		}

		// if(this.goodsInfoBean == null)
		this.goodsInfoBean = new goodsInfoBean();
		this.goodsInfoBean.setData(data.goodsInfo, data.img_ls);
		this.initPutOnList(this.goodsInfoBean.onsale_yds); //编辑产品
		this.onsaleYds = data.onsale_yds;

		let qtyobj = this.getQty();

		if (qtyobj) {
			this.mapQty = this.setQtyvalueFun(qtyobj.qty)
			this.saveGB.qty = qtyobj;
		}
		if (this.goodsInfoBean.supp_name)
			this.saveGB.supp_name = this.goodsInfoBean.supp_name;

		if (this.goodsInfoBean.suppid)
			this.saveGB.suppid = this.goodsInfoBean.suppid;

		if (this.goodsInfoBean.id)
			this.saveGB.goods_id = this.goodsInfoBean.id;

		if (this.goodsInfoBean.title)
			this.saveGB.title = this.goodsInfoBean.title;

		if (this.goodsInfoBean.item_no)
			this.saveGB.item_no = this.goodsInfoBean.item_no;

		if (this.goodsInfoBean.barcode)
			this.saveGB.barcode = this.goodsInfoBean.barcode;

		if (this.goodsInfoBean.comment)
			this.saveGB.comm = this.goodsInfoBean.comment;

		if (this.goodsInfoBean.qty_warn)
			this.saveGB.qty_warn = this.goodsInfoBean.qty_warn;

		if (this.goodsInfoBean.buymin)
			this.saveGB.buymin = this.goodsInfoBean.buymin;

		if (this.goodsInfoBean.desc)
			this.saveGB.desc = this.goodsInfoBean.desc;

		if (this.goodsInfoBean.txt_share)
			this.saveGB.txt_share = this.goodsInfoBean.txt_share;

		if (this.goodsInfoBean.imgs_base) {
			let arr = this.goodsInfoBean.imgs_base;

			if (arr) {
				let len = arr.length;
				for (let i = 0; i < len; i++) {
					let obj = {};
					if (i == 0) {
						obj.type = 1;
					} else {
						obj.type = 0;
					}
					obj.image = arr[i];
					this.saveGB.imgs.push(obj);
				}
			}
		}
	} else {
		if (data.actType == staticConst.ACT_GOODS_ADD) //新增产品
		{
			this.goodsInfoBean = null;
			this.saveGB = new saveGdsBean();
			if (data.keyCode) {
				this.saveGB.barcode = data.keyCode;
				this.keyCode = data.keyCode;
			}
			this.saveGB.act = staticConst.ATCTION_GOODS_ADD; //操作：add=新增，
		}
		//初始化上架列表
		if (data.hasOwnProperty('onsale_yds')) //新增产品，
		{
			this.initPutOnList(data.onsale_yds, true);
			this.onsaleYds = data.onsale_yds;
		}
	}
	await this.loadConfInfo();
	uni.$emit(eventCtrl.SHOW_ADD_GOODS_EVENT);
}

/****初始化上架列表数据**isNew是否是新增产品***/
goodsCtrl.initPutOnList = function (data, isNew = false) {
	let goodsBean = this.goodsInfoBean;
	let appSale = goodsBean ? goodsBean.onsale : 2;
	let list = [];
	let noSale = {};
	noSale.id = -1;
	noSale.name = lang.putOnsaleTxt.noSaleTxt; //"不上架";
	let app = {};
	app.id = 0;
	app.name = lang.putOnsaleTxt.appTxt; //"APP上架";
	if (isNew) {
		noSale.on = 2;
		app.on = 1;
		appSale = app.on;
	} else {
		//编辑产品
		noSale.on = appSale == 1 ? 2 : 1;
		app.on = appSale;
	}
	this.saveGB.onsale = appSale;
	if (data) {
		let len = data.length;
		for (let i = 0; i < len; i++) {
			let obj = data[i];
			let d = {};
			d.id = obj.idyun;
			d.name = obj.name;
			d.on = obj.onsale_yd;
			if (noSale.on == 1)
				noSale.on = obj.onsale_yd == 1 ? 2 : 1;
			if (obj.onsale_yd == 1) {
				this.saveGB.onsale_yds.push(d.id);
			}
			list.push(d);
		}
	}
	if (isNew) {
		list.unshift(app);

	}
	list.unshift(noSale);
	this.putOnList = list;
}

/***获取产品信息****/
goodsCtrl.getGoodsInfoBean = function () {
	return this.goodsInfoBean;
}

/*****品牌***BEGIN**/
goodsCtrl.getBrandList = function () {
	let barList = this.setBrandItem()
	return barList;
}
//给品牌添加  checked  属性判断选中样式
goodsCtrl.setBrandItem = function () {
	let that = this;
	let temp = [];
	if (this.goodsInfoBean)
		temp = this.goodsInfoBean.brands;
	this.brandList = this.setItem(this.brandList, temp);

	if (that.searSeBrandArr && that.searSeBrandArr.length) {
		getSearSeBrandArr();
		this.brandList = util.removeDuplicate(this.brandList, 'id');
	}
	this.brandList = this.brandList.sort(util.sorts_fun("checked", true));
	return this.brandList;

	function getSearSeBrandArr() {
		let len = that.searSeBrandArr.length;
		for (let i = 0; i < len; i++) {
			let o = that.searSeBrandArr[i];
			let temp = ssBArr(o.id);
			o.checked = true;
			if (temp) {
				that.brandList[i] = o;
			} else {
				that.brandList.unshift(o)
			}
		}
	}

	function ssBArr(id) {
		let len = that.brandList.length;
		for (let i = 0; i < len; i++) {
			let o = that.brandList[i];
			if (o.id == id) {
				return o;
			}
		}
		return null;
	}

}
//选中和删除品牌
goodsCtrl.changeBrands = function (item, index) {
	let that = this;
	let obj = this.changeItem(this.brandList, item, index, staticConst.MAX_BRANDS);
	updateSearBrandArr(obj);
	return obj;

	function updateSearBrandArr(o) {
		if (o.checked && that.searSeBrandArr.length == 0) {
			// that.searSeBrandArr.push
			that.searSeBrandArr.push(o);
			return;
		}
		let idx = -1;
		for (let i = 0; i < that.searSeBrandArr.length; i++) {
			let data = that.searSeBrandArr[i];
			if (data.id == o.id) {
				idx = i;
			}
		}

		if (idx >= 0) {
			if (o.checked) {
				that.searSeBrandArr[idx] = o;
			} else {
				that.searSeBrandArr.splice(idx, 1);
			}
		} else {
			if (o.checked)
				that.searSeBrandArr.push(o);
		}
	}
}


/*****品牌***END**/

/******公共方法***BEGIN**/
/****设置Item数据*****/
goodsCtrl.setItem = function (tempArr, arr) {
	if (tempArr) {
		let len = tempArr.length;
		for (let i = 0; i < len; i++) {
			let obj = tempArr[i];
			if (arr && arr.length > 0) {
				obj.checked = checked(obj.id, arr);
			} else {
				obj.checked = false;
			}
		}
	}

	function checked(id, arr) {
		let len = arr.length;
		for (let j = 0; j < arr.length; j++) {
			if (id == arr[j])
				return true;
		}
		return false;
	}

	return tempArr;
}

/*****选择改变****/
goodsCtrl.changeItem = function (tempArr, item, index, max, isTag) {
	let checked = item.checked;
	if (checked) {
		item.checked = !checked;
		tempArr[index] = item;
		return item;
	} else {
		let isMoreBrand = goodsTlpCtrl.getTplBrandMoreLimit();
		let len = tempArr.length;
		let count = 0;
		for (let i = 0; i < len; i++) {
			let obj = tempArr[i];
			if (obj.checked) {
				count++;

			}
		}

		if (!isTag && !isMoreBrand && count >= 1) //单个品牌
		{
			for (let i = 0; i < len; i++) {
				let obj = tempArr[i];
				obj.checked = false;
			}
			item.checked = !checked;
			tempArr[index] = item;
			return item;
		}

		if (count >= max) {
			that.$msg({
				title: isTag ? lang.tagTxt.moreThanMax : lang.brandTxt.moreThanMax
			});
			return item;
		}
		item.checked = !checked;
		tempArr[index] = item;
		return item;
	}
}
/******公共方法***END**/


/***** 类别 *** BEGIN **/
goodsCtrl.getCatList = function () {
	return this.catList;
}

/****初始化分类数据*****/
goodsCtrl.initCat = function () {
	let arr = this.catList;
	let gdInfo = this.goodsInfoBean;
	this.firstLevelCat = [];
	this.secondLevelCat = [];
	this.thirdLevelCat = [];
	this.saveGB.catid = "";
	this.category.catid1 = {};
	if (arr && arr.length) {
		let len = arr.length;
		for (let i = 0; i < len; i++) {
			let o = {};
			let temp = arr[i];
			o.id = temp.id;
			o.fid = 0;
			o.title = temp.title;
			o.level = 0;
			if (gdInfo && gdInfo.catid1 && temp.id == gdInfo.catid1) {
				o.checked = true;
				this.saveGB.catid = o.id;
				this.category.catid1 = o;
			} else {
				o.checked = false;
			}
			this.firstLevelCat.push(o);
			if (temp.son) {

				let arr1 = temp.son;
				for (let j = 0; j < arr1.length; j++) {
					let oo = {};
					let temp1 = arr1[j];
					oo.id = temp1.id;
					oo.fid = o.id;
					oo.title = temp1.title;
					oo.level = 1;
					if (gdInfo && gdInfo.catid2 && temp1.id == gdInfo.catid2) {
						oo.checked = true;
						this.saveGB.catid = oo.id;
						this.category.catid2 = oo;
					} else {
						oo.checked = false;
					}
					this.secondLevelCat.push(oo);
					if (temp1.son) {
						let arr2 = temp1.son;
						for (let k = 0; k < arr2.length; k++) {
							let ooo = {};
							let temp2 = arr2[k];
							ooo.id = temp2.id;
							ooo.fid = oo.id;
							ooo.title = temp2.title;
							ooo.level = 2;
							if (gdInfo && gdInfo.catid && temp2.id == gdInfo.catid) {
								ooo.checked = true;
								this.saveGB.catid = ooo.id;
								this.category.catid = ooo;
							} else {
								ooo.checked = false;
							}
							this.thirdLevelCat.push(ooo);
						}
					}
				}
			}
		}
	}
}

/*****获取一级分类列表*******/
goodsCtrl.getFirstCatList = function () {
	let catTxt = lang.getCatTxt();
	let ft = {};
	ft.title = catTxt.firstSpecTitTxt;
	ft.isFirstLevel = true;
	ft.hasSon = true;
	ft.id = 0;
	let arr = [];
	arr.push(ft);

	//一级
	let data = {};
	data.titleList = arr;
	data.list = this.firstLevelCat;
	return data;
}

/***获取二级分类****/
goodsCtrl.getSecondCatList = function (id) {
	//二级级
	let data = this.getSecondAndThirdList(this.secondLevelCat, id, this.category.catid1.title);
	return data;
}

/*****获取三级分类*****/
goodsCtrl.getThirdCatList = function (id) {
	let data = this.getSecondAndThirdList(this.thirdLevelCat, id, this.category.catid2.title);
	return data;
}

/****获取二级三级数据***/
goodsCtrl.getSecondAndThirdList = function (tempArr, id, title) {
	let temp = [];
	let len = tempArr.length;
	for (let i = 0; i < len; i++) {
		let obj = tempArr[i];
		if (obj.fid == id) {
			temp.push(obj);
		}
	}

	let ft = {};
	ft.title = title;
	ft.isFirstLevel = true;
	ft.hasSon = true;
	ft.id = 0;
	let arr = [];
	arr.push(ft);

	//二级级
	let data = {};
	data.titleList = arr;
	data.list = temp;
	return data;
}

/****点击分类处理数据*****/
goodsCtrl.onClickCatItem = function (data) {
	let cat = this.category;
	this.saveGB.catid = data.id;
	if (data.level == 0) {
		// if(data.id == cat.catid1.id)return;
		checked(this.firstLevelCat, data);
		checked(this.secondLevelCat, data);
		checked(this.thirdLevelCat, data);

		if (data.checked) {
			this.category.catid = data;
			this.category.catid1 = data;
			this.category.catid2 = null;
		} else {
			this.category.catid = null;
			this.category.catid1 = null;
			this.category.catid2 = null;
		}
	} else if (data.level == 1) {
		checked(this.secondLevelCat, data);
		checked(this.thirdLevelCat, data);
		if (data.checked) {
			this.category.catid2 = data;
			this.category.catid = data;
		} else {
			this.category.catid2 = null;
			this.category.catid = null;
		}
	} else {
		checked(this.thirdLevelCat, data);
		if (data.checked) {
			this.category.catid = data;
		} else {
			this.category.catid = null;
		}
	}

	function checked(arr, d) {
		let len = arr.length;
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
			if (obj.id == d.id) {

				obj.checked = d.checked;
			} else {
				obj.checked = false;
			}
		}
	}
}

/***获取选中的分类的子分类***/
goodsCtrl.getCategory = function () {
	return this.category;
}
/***** 类别 *** END **/

/****获取渲染标签列表***BEGIN**/
goodsCtrl.getTagList = function () {
	let temp = [];
	if (this.goodsInfoBean)
		temp = this.goodsInfoBean.tags;
	this.tagList = this.setItem(this.tagList, temp);
	return this.tagList;
}

/***点击标签改变数据******/
goodsCtrl.changeTag = function (item, index) {
	let obj = this.changeItem(this.tagList, item, index, staticConst.MAX_TAGS, true);
	return obj;
}
/********标签***END**/

/***************初始库存**********BEGIN*****/
goodsCtrl.getStoreList = function () {
	return this.storeList;
}


/****初始库存获取获取单位列表***/
goodsCtrl.getStoreUnit = function () {
	return this.selectedUnitArr;
}


/**********初始库存获取获选中的规格**************/
goodsCtrl.getCheckedStoreArr = function () {
	if (!this.specList) return;
	this.checkedStoreSpecArr = [];
	let len = this.specList.length;
	for (let i = 0; i < len; i++) {
		let obj = this.specList[i];
		if (obj.checked) {
			if (obj.son) {
				let arr = obj.son;
				let has = checked(arr); //检查子节点是否有选中的规格
				if (has) {
					for (let j = 0; j < arr.length; j++) {
						let obj1 = arr[j];
						if (obj1.checked) {
							if (obj1.son) {
								let arr1 = obj1.son;
								let has1 = checked(arr1); //检查子节点是否有选中的规格
								if (has1) {
									for (let k = 0; k < arr1.length; k++) {
										let obj2 = arr1[k];
										if (obj2.checked) {
											let o = {};
											o.titles = obj.title + ", " + obj1.title + ", " + obj2.title; //压入当前规格与父规格的title,id 到数组
											o.ids = obj.id + "|" + obj1.id + "|" + obj2.id;
											this.checkedStoreSpecArr.push(o);
										}
									}
								} else //没有直接压入当前规格与父规格的title,id 到数组
								{
									let o = {};
									o.titles = obj.title + "," + obj1.title;
									o.ids = obj.id + "|" + obj1.id;
									this.checkedStoreSpecArr.push(o);
								}
							} else //没有直接压入当前规格与父规格的title,id 到数组
							{
								let o = {};
								o.titles = obj.title + "," + obj1.title;
								o.ids = obj.id + "|" + obj1.id;
								this.checkedStoreSpecArr.push(o);
							}
						}
					}
				} else //没有直接压入当前规格的title,id 到数组
				{
					let o = {};
					o.titles = obj.title;
					o.ids = obj.id;
					this.checkedStoreSpecArr.push(o);
				}
			} else //没有直接压入当前规格的title,id 到数组
			{
				let o = {};
				o.titles = obj.title;
				o.ids = obj.id;
				this.checkedStoreSpecArr.push(o);
			}
		}
	}

	return this.checkedStoreSpecArr;

	//检测是否有选中的规格
	function checked(arr) {
		for (let i = 0; i < arr.length; i++) {
			let obj = arr[i];
			if (obj.checked) {
				return true;
			}
		}
		return false;
	}
}



/****获取库存，库存为有填写库不显示，创建或库存为0显示***/
goodsCtrl.getQty = function () {
	let qty = null;
	if (this.goodsInfoBean && this.goodsInfoBean.qty) {
		if (typeof (this.goodsInfoBean.qty) == 'string')
			qty = JSON.parse(this.goodsInfoBean.qty);
		else
			qty = this.goodsInfoBean.qty;
	}
	return qty;
}

/****草稿数据提取******/
goodsCtrl.initDeriftMapQty = function (mapQty) {
	if (mapQty) {
		this.mapQty = JSON.parse(JSON.stringify(mapQty));
	}
}

goodsCtrl.changeQty = function (data) {
	this.mapQty[data.id] = data.num;
}

goodsCtrl.getMapQty = function () {
	return this.mapQty;
}

goodsCtrl.setQtyvalueFun = function (data) {
	let obj = {}
	data = data || [];
	data.map(v => {
		v.list = v.list || [];
		v.list.map(vs => {
			vs.list.map(vw => {
				obj[`${v.storeid}_${vs.specid}_${vw.unitid}`] = vw.num
			})
		})
	})
	return obj
}
/***************初始库存**********END*****/


/***** 经手人 *** BEGIN **/
goodsCtrl.getUcpList = function () {
	let ucpList = this.setUcpList();
	return ucpList;
}

/****设置经手人****/
goodsCtrl.setInitUcp = async function () {
	let uid_cp = "";
	if (this.goodsInfoBean) {
		uid_cp = this.goodsInfoBean.uid_cp;
	} else {
		let user = await localStore.getUserInfo();
		if (user) {
			uid_cp = user.id;
		}
	}
	if (this.saveGB && uid_cp != "")
		this.saveGB.uid_cp = uid_cp;

	let ucpObj = null;
	if (this.ucpList) {
		for (let i = 0; i < this.ucpList.length; i++) {
			if (this.ucpList[i].uid == uid_cp) {

				ucpObj = this.ucpList[i];
				break;
			}
		}
		if (!ucpObj) {
			ucpObj = this.ucpList[0];
		}
	}
	return ucpObj;
}

//getUidCp = 1无操作   ==2时 返回产品默认选中的经手人
goodsCtrl.setUcpList = function () {

	if (this.ucpList) {

		let dealData = this.ucpList.forEach((item, index) => {
			item.title = item.name;
			item.id = item.uid;
		});
	}
	return this.ucpList;
}

goodsCtrl.setDealer = function (item) {
	this.saveGB.uid_cp = item.uid;
}

goodsCtrl.setSupplier = function (item) {
	this.saveGB.suppid = item.id;
	this.saveGB.supp_name = item.name;

}

/***** 经手人 *** END **/

/***** 上架 *** BEGIN **/
/******获取上架列表********/
goodsCtrl.getPutOnList = function () {
	return this.putOnList;
}

/******改变上架******/
goodsCtrl.putOnChange = function (item) {
	let that = this;
	if (item.id < 0) {
		this.saveGB.onsale = 2;
		this.saveGB.onsale_yds = [];
		setPutOn(true, this.putOnList, item);
	} else {
		if (item.id == 0) {
			this.saveGB.onsale = item.on;
		} else {
			if (item.on == 1) {
				this.saveGB.onsale_yds.push(item.id);
			} else {
				let len = this.saveGB.onsale_yds.length;
				for (let i = 0; i < len; i++) {
					if (item.id == this.saveGB.onsale_yds[i]) {
						this.saveGB.onsale_yds.splice(i, 1);
						break;
					}
				}
			}
		}
		setPutOn(false, this.putOnList, item);
	}
	/***点击下架处理****/
	function setPutOn(isOff, arr, item) {
		let len = arr.length;
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
			if (obj.id == item.id) {
				obj.on = item.on;
				if (!isOff) {
					arr[0].on = 2;
					break;
				}
			} else {
				if (isOff)
					obj.on = 2;
			}
		}
	}
}
/***** 上架 *** END **/


/********备注****BEGIN****/
goodsCtrl.sesRemarks = function (value) {
	this.saveGB.comm = value;
}
/********备注****BEGIN****/


/****输入分享文字******/
goodsCtrl.inputShareTxt = function (value) {
	this.saveGB.txt_share = String(value).trim();
}

/*****输入警戒库存*****/
goodsCtrl.inputWarnQty = function (value) {
	this.saveGB.qty_warn = value;
}

/****最小起订量****/
goodsCtrl.inputBuymin = function (value) {
	this.saveGB.buymin = value;
}

/************************ 更多产品详情  BEGIN ********************/
goodsCtrl.getAttrList = function () {
	return this.attrList;
}

/***初始化产品更多详情Tab****/
goodsCtrl.initGdMoreTab = function () {
	let arr = [];
	this.moreGdDetail = [];
	if (this.attrList) {
		let len = this.attrList.length;
		for (let i = 0; i < len; i++) {
			let obj = this.attrList[i];
			if (obj.status == 1) {
				arr.push(obj);
				this.initGdMorePhoTxt(obj.id, obj.title);
			}
		}
	}
	return arr;
}

/*****初始化详情块图文******/
goodsCtrl.initGdMorePhoTxt = function (id, title) {
	let that = this;
	let gdInfoBean = this.goodsInfoBean;
	// this.moreGdDetail = [];
	if (gdInfoBean && gdInfoBean.desc) {
		let len = gdInfoBean.desc.length;
		for (let i = 0; i < len; i++) {
			let obj = gdInfoBean.desc[i];
			if (obj.id == id) {
				if (obj.desc && obj.desc.list) {
					let o = {};
					o.id = id;
					o.title = title;
					o.list = formatPhotoData(obj.desc.list, id);
					this.moreGdDetail.push(o);
				}
			}
		}
	}

	function checkHas(arr, id) {
		let len = arr.length;
		if (len == 0) return false;
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
		}
	}

	function formatPhotoData(arr, id) {
		let len = arr.length;
		let tempArr = [];
		for (let i = 0; i < len; i++) {
			let obj = arr[i];
			let o = {};
			o.desc = obj.desc;
			o.id = id;
			// let pic = String(obj.img).replace(',w_1280', ',w_160');
			let pic = String(obj.img).replace(',w_1280', ',w_160');
			o.list = [{
				url: pic,
				// imgs_base: obj.img_rel,
				imgs_base: obj.img,
				imgs: obj.img,
				isDel: true
			}];
			tempArr.push(o);
		}
		return tempArr;
	}
}

/****获取更多产品详情描述和图片****/
goodsCtrl.getGdMoreDesc = function (id) {
	let desc = null;
	let len = this.moreGdDetail.length;
	console.log('this.moreGdDetail====================>', this.moreGdDetail);
	for (let i = 0; i < len; i++) {
		let obj = this.moreGdDetail[i];
		if (obj.id == id) {
			desc = obj.list;
			break;
		}
	}
	return desc;
}

/****添加一块图文****/
goodsCtrl.addGdMoreDetailPhoTxt = function (data) {
	let len = this.moreGdDetail.length;
	let tempArr = [];
	let has = false;
	for (let i = 0; i < len; i++) {
		let obj = this.moreGdDetail[i];
		if (obj.id == data.id) {
			has = true;
			let o = {};
			o.desc = "";
			o.id = data.id;
			o.list = [{
				url: "",
				imgs_base: "",
				imgs: "",
				isDel: false
			}];
			obj.list.push(o);
			tempArr = obj.list;
		}
	}

	if (!has) {
		let o = {};
		o.id = data.id;
		o.title = data.title;
		o.list = [];
		let oo = {};
		oo.desc = "";
		oo.id = data.id;
		oo.list = [{
			url: "",
			imgs_base: "",
			imgs: "",
			isDel: false
		}];
		o.list.push(oo)
		tempArr = o.list;
		this.moreGdDetail.push(o);
	}
	return tempArr;
}

/****删除一个图文块****/
goodsCtrl.delGdMoreDetailPhoTxt = function (id, index) {

	let len = this.moreGdDetail.length;
	let tempArr = [];
	for (let i = 0; i < len; i++) {
		let obj = this.moreGdDetail[i];
		if (obj.id == id) {
			obj.list.splice(index, 1);
			tempArr = obj.list;
		}
	}
	return tempArr;
}

/*********删除产品详情的列表的某个图片列表**暂时没有实现**/
goodsCtrl.delGdMoreDetailPhoto = function (id, parentIdx, index) {
	console.log();
	let len = this.moreGdDetail.length;
	let tempArr = [];
	for (let i = 0; i < len; i++) {
		// let obj = this.moreGdDetail[i];
	}
}
/*******暂时没有实现**/
goodsCtrl.uploadGdsMoreDetailPhoto = function (data) {
	let len = this.moreGdDetail.length;
	let tempArr = [];
	for (let i = 0; i < len; i++) {
		let obj = this.moreGdDetail[i];
		if (obj.id == data.tabId) {
			let o = obj.list[data.parentIdx];
			let oo = o.list[data.index];
			oo.url = data.url + staticConst.PHOTO_OSS_SMALL_160;
			oo.imgs = data.name;
			oo.imgs_base = data.url;
		}
	}
}
/************************ 更多产品详情  END ********************/

/*********是否可以编辑初始库存**********/
goodsCtrl.getShowQtyLimit = function () {

	let result = true;
	if (this.isEditGoods == staticConst.ACT_GOODS_COPY) return result

	if (this.goodsInfoBean) //如果有初始库存就不显示了
	{
		result = this.goodsInfoBean.qty ? false : true;
	}
	return result;
}

/**********保存草稿**************/
goodsCtrl.saveDerift = async function () {
	let that = this;
	this.saveGB.spec_ls = this.saveSpec(); //规格
	this.saveGB.prices = this.savePrice(); //价格
	this.saveGB.costs = this.saveCosts(); //成本
	this.saveGB.qty = this.savaQty();
	this.saveGB.unit_ls = this.saveUnit(); //单位
	this.saveGB.brands = this.saveBrandsTags(this.brandList); //品牌
	this.saveGB.tags = this.saveBrandsTags(this.tagList);
	this.saveGB.desc = this.saveMoreDetail();

	let gdInfo = {};
	if (!this.isSys) {
		let photoObj = gdPhoto();
		gdInfo.imgs = photoObj.imgs;
		gdInfo.imgs_base = photoObj.imgs_base;
		gdInfo.imgs_s = photoObj.imgs_s;
	} else {
		gdInfo.img_ls = gdSysPhoto(); //爱搜
	}


	gdInfo.title = this.saveGB.title;
	gdInfo.item_no = this.saveGB.item_no;
	gdInfo.barcode = this.saveGB.barcode;
	gdInfo.spec_ckls = this.saveGB.spec_ls;
	gdInfo.spec_ls = this.saveGB.spec_ls;
	gdInfo.unit_ls = this.saveGB.unit_ls;
	gdInfo.priceinfo = this.saveGB.prices;
	gdInfo.costInfo = this.saveGB.costs;
	// gdInfo.imgs_s = 

	let cat = draftCat();
	if (cat) {
		gdInfo.catid = cat.catid;
		gdInfo.catid1 = cat.catid1;
		gdInfo.catid2 = cat.catid2;
	} else {
		gdInfo.catid = 0;
		gdInfo.catid1 = 0;
		gdInfo.catid2 = 0;
	}
	gdInfo.qty = this.saveGB.qty;
	gdInfo.qty_warn = this.saveGB.qty_warn;
	gdInfo.buymin = String(this.saveGB.buymin);
	gdInfo.comment = this.saveGB.comm;
	gdInfo.txt_share = this.saveGB.txt_share;
	gdInfo.uid_cp = this.saveGB.uid_cp;

	gdInfo.isqty = 2; //草稿显示库存；

	if (this.saveGB.brands)
		gdInfo.brands = this.saveGB.brands;
	else
		gdInfo.brands = null;

	if (this.saveGB.tags)
		gdInfo.tags = this.saveGB.tags;
	else
		gdInfo.tags = null;

	gdInfo.onsale = this.saveGB.onsale;

	gdInfo.onsale_yds = ydList(this.onsaleYds);

	if (this.mapQty)
		gdInfo.mapQty = JSON.parse(JSON.stringify(this.mapQty));

	gdInfo.desc = desc(); //that.saveGB.desc;

	await localStore.setGoodsDraft(gdInfo, '1');

	///****草稿分类*****/
	function draftCat() {
		let d = null;
		if (that.saveGB.catid != "") {
			d = {};
			let cId = that.saveGB.catid;
			let arr = that.catList; {
				for (let i = 0; i < arr.length; i++) {
					let temp = arr[i];
					d.catid1 = temp.id;
					if (temp.id == cId) {
						d.catid2 = 0;
						d.catid = cId;
						return d;
					}
					if (temp.son) {
						let arr1 = temp.son;
						for (let j = 0; j < arr1.length; j++) {
							let temp2 = arr1[j];
							d.catid2 = temp2.id;
							if (temp2.id == cId) {
								d.catid = temp2.id;
								return d;
							}
							if (temp2.son) {
								let arr2 = temp2.son;
								for (let k = 0; k < arr2.length; k++) {
									let temp3 = arr2[k];
									if (temp3.id == cId) {
										d.catid = temp3.id;
										return d;
									}
								}
							}
						}
					}
				}
			}
		}
		return d;
	}

	function ydList(onsale_yds) {
		let arr = that.saveGB.onsale_yds;
		if (arr && arr.length > 0) {
			let len = arr.length
			for (let i = 0; i < len; i++) {
				for (let j = 0; j < onsale_yds.length; j++) {
					let obj = onsale_yds[j];
					if (obj.idyun == arr[i]) {
						obj.onsale_yd = 1;
					}
				}
			}
		}
		return onsale_yds;
	}

	/***非爱搜企业照片****/
	function gdPhoto() {
		let imgs = [];
		let imgs_base = [];
		let imgs_s = [];
		let tempArr = that.saveGB.imgs;
		let len = tempArr.length;
		for (let i = 0; i < len; i++) {
			let obj = tempArr[i]; //[{type: 1, image: "d/cp25/goods/dc745f0fd9e3db9c33b82d8821bf2308.jpg"}]
			imgs.push(config.OSS_HOST + obj.image + staticConst.PHOTO_OSS_BIG_1280);
			imgs_base.push(obj.image);
			imgs_s.push(config.OSS_HOST + obj.image + staticConst.PHOTO_OSS_SMALL_160);
		}
		let data = {};
		data.imgs = imgs;
		data.imgs_base = imgs_base;
		data.imgs_s = imgs_s;
		return data;
	}

	/*****爱搜企业****/
	function gdSysPhoto() {
		let len = that.gdPhotoArr.length;
		let imgLs = [];
		for (let i = 0; i < len; i++) {
			let sku = that.gdPhotoArr[i];
			if (!sku.imgs || !sku.imgs_base) continue;
			let data = {};
			data.img = sku.imgs;
			data.img_base = sku.imgs_base;
			if (i == 0)
				data.type = 1;
			else
				data.type = 0;
			if (sku.skuIds) {
				let arr = String(sku.skuIds).split(",");
				let res = getSpecUnitIdsTitle(arr);
				data.spec_unit_ids = res.spec_unit_ids;
				data.spec_unit_txt = res.spec_unit_txt;
			} else {
				data.spec_unit_ids = [];
				data.spec_unit_txt = [];
			}
			imgLs.push(data);
		}
		return imgLs;

	}

	function getSpecUnitIdsTitle(arr) {
		let spec_unit_ids = [];
		let spec_unit_txt = [];
		for (let i = 0; i < arr.length; i++) {
			let data = null;
			let t = String(arr[i]).split("_");
			if (t.length == 2) //规矩 _单位
			{
				data = that.checkSpecTitle(t[1], t[0]);
			} else //单位
			{
				data = that.checkSpecTitle(t[0]);
			}

			if (data) {
				spec_unit_ids.push(data.ids);
				spec_unit_txt.push(data.titles);
			}
		}

		let res = {};
		res.spec_unit_ids = spec_unit_ids;
		res.spec_unit_txt = spec_unit_txt;
		return res;
	}


	/******产品详情*****/
	function desc() {
		let desc = that.saveGB.desc;
		if (desc) {
			for (let i = 0; i < desc.length; i++) {
				let obj = desc[i];
				if (obj && obj.desc && obj.desc.list) {
					let arr = obj.desc.list;
					for (let j = 0; j < arr.length; j++) {
						let obj1 = arr[j];
						obj1.img_rel = config.OSS_HOST + obj1.img + staticConst.PHOTO_OSS_SMALL_160;
						arr[j] = obj1;
					}
				}
			}
		}
		return desc;
	}
}

/******清除草稿*****/
goodsCtrl.clearDerift = async function () {
	await localStore.removeGoodsDraft();
}

/****获取是否有草稿***/
goodsCtrl.getDerift = async function () {
	let data = await localStore.getGoodsDraft('1');
	return data;
}

/****编辑保存的草稿产品*****/
goodsCtrl.editDraftToGoods = async function (data) {
	let goodsInfo = await this.getDerift();
	data.goodsInfo = goodsInfo;
	data.actType = staticConst.ACT_GOODS_DERIFT;
	this.setData(data);
}


export default goodsCtrl;